/**
 *<p>Copyright: Copyright (c) 2012</p> 
 *<p>Company: SCPII</p>
 *<p>Project:scpii-api</p>
 *@author xiezhouyan
 *@Date 2013-2-6 上午12:38:08
 *@version 1.0
 *@description:
 */
package com.scpii.api.common.error;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.ConversionNotSupportedException;
import org.springframework.beans.TypeMismatchException;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.validation.BindException;
import org.springframework.web.HttpMediaTypeNotAcceptableException;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.multipart.support.MissingServletRequestPartException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver;
import org.springframework.web.servlet.mvc.multiaction.NoSuchRequestHandlingMethodException;

import com.scpii.api.common.exception.ClientException;
import com.scpii.api.common.exception.InvalidMethodException;
import com.scpii.api.common.exception.InvalidPageException;
import com.scpii.api.common.exception.InvalidRequestException;
import com.scpii.api.common.exception.ServerErrorException;

public class ClientExceptionResolver extends AbstractHandlerExceptionResolver {

	protected final Log logger = LogFactory
			.getLog(ClientExceptionResolver.class);

	@Override
	protected ModelAndView doResolveException(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex) {
		if (ex instanceof ClientException) {
			return null;
		}
		if (ex instanceof NoSuchRequestHandlingMethodException) {
			throw new InvalidPageException(ex.getMessage(), ex);
		} else if (ex instanceof HttpRequestMethodNotSupportedException) {
			throw new InvalidMethodException(ex.getMessage(), ex);
		} else if (ex instanceof HttpMediaTypeNotSupportedException) {
			throw new ServerErrorException(ex.getMessage(), ex);
		} else if (ex instanceof HttpMediaTypeNotAcceptableException) {
			throw new ServerErrorException(ex.getMessage(), ex);
		} else if (ex instanceof MissingServletRequestParameterException) {
			throw new ServerErrorException(ex.getMessage(), ex);
		} else if (ex instanceof ServletRequestBindingException) {
			throw new InvalidRequestException(ex.getMessage(), ex);
		} else if (ex instanceof ConversionNotSupportedException) {
			throw new ServerErrorException(ex.getMessage(), ex);
		} else if (ex instanceof TypeMismatchException) {
			throw new InvalidRequestException(ex.getMessage(), ex);
		} else if (ex instanceof HttpMessageNotReadableException) {
			throw new ServerErrorException(ex.getMessage(), ex);
		} else if (ex instanceof HttpMessageNotWritableException) {
			throw new ServerErrorException(ex.getMessage(), ex);
		} else if (ex instanceof MethodArgumentNotValidException) {
			throw new ServerErrorException(ex.getMessage(), ex);
		} else if (ex instanceof MissingServletRequestPartException) {
			throw new ServerErrorException(ex.getMessage(), ex);
		} else if (ex instanceof BindException) {
			throw new InvalidRequestException(ex.getMessage(), ex);
		} else {
			if (ex != null) {
				throw new ServerErrorException(ex.getMessage(), ex);
			}
		}
		return null;
	}
}
